package com.honetooyoung.JavaW.myservlet;


import com.honetooyoung.JavaW.jdbcUtil.jUtils;
import jakarta.servlet.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class myServlet implements Servlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter writer=response.getWriter();

        //jcbc 代码
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            connection= jUtils.getConnection();
            String sql="select * from emp";
            ps=connection.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()){
                String empNo= rs.getString(1);
                String name= rs.getString(2);
                String sal=rs.getString(6);
                System.out.println(empNo+" "+name+" "+sal);
                writer.print(empNo+" "+name+" "+sal+"<br>");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            jUtils. close(connection,ps,rs);
        }

    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
